extends Control

@onready var main_menu_panel = $MainMenuPanel
@onready var level_select_panel = $LevelSelectPanel
@onready var settings_panel = $SettingsPanel

func _ready():
	# 开始时只显示主菜单
	show_panel("main")
	
	# 连接主菜单按钮信号
	$MainMenuPanel/StartButton.pressed.connect(_on_start_button_pressed)
	$MainMenuPanel/SettingsButton.pressed.connect(_on_settings_button_pressed)
	$MainMenuPanel/QuitButton.pressed.connect(_on_quit_button_pressed)
	
	# 连接关卡选择界面按钮信号
	$LevelSelectPanel/Level1Button.pressed.connect(_on_level1_button_pressed)
	$LevelSelectPanel/Level2Button.pressed.connect(_on_level2_button_pressed)
	$LevelSelectPanel/Level3Button.pressed.connect(_on_level3_button_pressed)
	$LevelSelectPanel/BackButton.pressed.connect(_on_back_to_main_pressed)
	
	# 连接设置界面按钮信号
	$SettingsPanel/BackButton.pressed.connect(_on_back_to_main_pressed)
	$SettingsPanel/MusicSlider.value_changed.connect(_on_music_slider_changed)
	$SettingsPanel/SoundSlider.value_changed.connect(_on_sound_slider_changed)

func show_panel(panel_name):
	# 隐藏所有面板
	main_menu_panel.hide()
	level_select_panel.hide()
	settings_panel.hide()
	
	# 显示指定面板
	match panel_name:
		"main":
			main_menu_panel.show()
		"levels":
			level_select_panel.show()
		"settings":
			settings_panel.show()

# 主菜单按钮功能
func _on_start_button_pressed():
	show_panel("levels")

func _on_settings_button_pressed():
	show_panel("settings")

func _on_quit_button_pressed():
	get_tree().quit()

# 关卡选择按钮功能
func _on_level1_button_pressed():
	print("正在加载关卡 1...")
	# 这里替换为实际加载场景的代码
	# get_tree().change_scene_to_file("res://Level1.tscn")

func _on_level2_button_pressed():
	print("正在加载关卡 2...")
	# get_tree().change_scene_to_file("res://Level2.tscn")

func _on_level3_button_pressed():
	print("正在加载关卡 3...")
	# get_tree().change_scene_to_file("res://Level3.tscn")

func _on_back_to_main_pressed():
	show_panel("main")

# 设置功能
func _on_music_slider_changed(value):
	print("音乐音量设置为: ", value)
	# 这里可以连接实际的音频总线
	# AudioServer.set_bus_volume_db(AudioServer.get_bus_index("Music"), linear_to_db(value))

func _on_sound_slider_changed(value):
	print("音效音量设置为: ", value)
	# AudioServer.set_bus_volume_db(AudioServer.get_bus_index("SFX"), linear_to_db(value))
